$(function(){
	$("#searchParamConfig_dategrid").datagrid_init;
	/*==初始化表格*/
	$('#searchParamConfig_dategrid').datagrid({
		loadMsg : "数据加载中",
		border:false,
		fit : true,
		title : "参数配置",
		fitColumns : true,
		pagination : true,
        emptyMsg : "数据加载完成,当前无数据!",
		rownumbers : true,
		singleSelect : true,
		toolbar:"#searchParamConfig_dategrid_toolbar",
		columns : [ [
		    {field : 'id',				title : '主键',			width:50 , align:'center',hidden:true},
		    {field : 'password',				title : '',			width:50 , align:'center',hidden:true},
            {field:	 'checked',checkbox:true},
		    {field : '1',			title : '编码',			width:50 , align:'center',hidden:false},
		    {field : '2',			title : '值',			width:50 , align:'center',hidden:false},
		    {field : '3',			title : '描述',			width:50 , align:'center',hidden:false}
		] ],
        onLoadSuccess:function(data){
            $(this).datagrid('doCellTip',{'max-width':'300px','delay':500});
        }
	});

	// var params = {};
	// $("#searchParamConfig_dategrid").datagrid_pagination(basepath+"/user/v1/selectUserList",JSON.stringify(params),"","" );

    /*查询按钮*/
	/*$("#searchParamConfig_btn_search").linkbutton({
		onClick:function(){
			var name = $("#searchParamConfig_Name").textbox("getValue");
			var params = {};
			params.username = name;
            $("#searchParamConfig_dategrid").datagrid_pagination(basepath+"/user/v1/selectUserList",JSON.stringify(params),"","" );
		}
	});*/

});

/**
 * 添加应用
 */
function addsearchParamConfig(){
    $('#searchParamConfig_username').textbox('setValue','');
    $('#searchParamConfig_password').textbox('setValue','');
    $('#searchParamConfig_repassword').textbox('setValue','');
    $('#searchParamConfig_id').val('');
    $('#searchParamConfigWindow').window({title:"添加应用"});
    $('#searchParamConfigWindow').window("open");
}

/**
 * 修改应用
 */
function updatesearchParamConfig(){
    var row = $('#searchParamConfig_dategrid').datagrid('getSelected');   //得到选中的行
    if(row == null){
        $.messager.alert("提示","请先选中一行！");
        return;
    }
    $('#searchParamConfig_id').val(row.id);
    $('#searchParamConfig_username').textbox('setValue',row.username);
    $('#searchParamConfig_password').textbox('setValue',row.password);
    $('#searchParamConfig_repassword').textbox('setValue',row.password);
    $('#searchParamConfigWindow').window({title:"修改应用"});
    $('#searchParamConfigWindow').window("open");
}

/**
 * 删除应用
 */
function deletesearchParamConfig(){
    var row = $('#searchParamConfig_dategrid').datagrid('getSelected');   //得到选中的行
    if(row == null){
        $.messager.alert("提示","请先选中一行！");
        return;
    }

    var params = {
        id : row.id,
        username : row.username,
        password : row.password
    };
    $.messager.confirm('确认','确认要删除【用户名】为【'+row.username+'】的用户么？',function(r) {
        if (r) {
            $.ajax({
                type: "post",
                url: path+'/user/v1/deleteUser',
                contentType : "application/json; charset=utf-8",
                datatype : "json",
                data: JSON.stringify(params),
                success: function(data){
                    if(data.result == 1){
//			    		$.messager.alert("提示","操作成功！");
                        searchUsers();
                    }else{
                        $.messager.alert("提示",data.msg);
                    }
                }
            });
        }
    });
}

/**
 * 新增修改保存
 */
function savesearchParamConfig(){
    var username =  $.trim($('#searchParamConfig_username').val());
    var password =  $.trim($('#searchParamConfig_password').val());
    var repassword =  $.trim($('#searchParamConfig_repassword').val());
    var id =  $.trim($('#searchParamConfig_id').val());
    if(password != repassword){
        $.messager.alert("提示","两次输入密码不一致，请重新输入！");
        return;
    }
    var params = {
        id : id,
        username : username,
        password : password
    };
    //异步获取数据到javascript对象，入参为查询条件和页码信息
    if(id != null && id !=""){
        $.ajax({
            type : "post",
            url : path+'/user/v1/updateuser',
            contentType : "application/json; charset=utf-8",
            datatype : "json",
            data :JSON.stringify(params),
            success: function(data){
                if(data.result == 1){
                    $.messager.alert("提示","修改操作成功！");
                    $('#searchParamConfigWindow').window('close');
                    searchUsers();
                }else{
                    $.messager.alert("提示",data.msg);
                }
            }
        });
    }else{
        $.ajax({
            type : "post",
            url : path+'/user/v1/add',
            contentType : "application/json; charset=utf-8",
            datatype : "json",
            data :JSON.stringify(params),
            success: function(data){
                if(data.result == 1){
                    $.messager.alert("提示","新增操作成功！");
                    $('#searchParamConfigWindow').window('close');
                    searchUsers();
                }else{
                    $.messager.alert("提示",data.msg);
                }
            }
        });
    }
}

/**
 * 查询所有用户
 */
function searchUsers() {
    var name = $("#searchParamConfig_Name").textbox("getValue");
    var params = {};
    params.username = name;
    $("#searchParamConfig_dategrid").datagrid_pagination(basepath+"/user/v1/selectUserList",JSON.stringify(params),"","" );
}
	